/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;

import com.us.usws.model.domain.system.MobileAuth;
import com.us.usws.model.flat.system.DOMobileAuth;

/**
 * DAO class
 * @author robot
 */
public class MobileAuthDAO {

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from sys_mobile_auth";

	private static final String SQL_GET_ALL_MOBILEAUTHS = "select * from sys_mobile_auth";

	private static final String SQL_GET_MOBILEAUTH_BY_PK = "select * from sys_mobile_auth where id = ?";

	/**
	 * CUR SQLs
	 */
	private static final String SQL_INSERT_MOBILEAUTH = "insert into sys_mobile_auth(mobile_no,create_date,auth_type_dsid,user_uid,ep_token,auth_code,end_date,authed_date) values (?,?,?,?,?,?,?,?)";

	private static final String SQL_UPDATE_MOBILEAUTH = "update sys_mobile_auth set mobile_no=?, create_date=?, auth_type_dsid=?, user_uid=?, ep_token=?, auth_code=?, end_date=?, authed_date=? where id=?";

	private static final String SQL_DEL_MOBILEAUTH = "delete from sys_mobile_auth where id=?";

	private static final String SQL_GET_MOBILE_AUTH_BY_AUTH_3 = "select * from sys_mobile_auth where mobile_no=? and auth_code=? and ep_token=? and authed_date is null order by create_date desc limit 0,1";
	private static final String SQL_GET_MOBILE_AUTH_BY_AUTH_2 = "select * from sys_mobile_auth where mobile_no=? and auth_code=? and authed_date is null order by create_date desc limit 0,1";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor();

	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<MobileAuth> getAllMobileAuths() {
		return dbe.list(SQL_GET_ALL_MOBILEAUTHS, MobileAuth.class);
	}

	/**
	 * Get all list by pagination.
	 */
	public List<MobileAuth> getMobileAuths(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_MOBILEAUTHS, null, start, end);
		return dbe.list(swp.getSql(), MobileAuth.class, swp.getParams());
	}

	/**
	 * Get MobileAuth by primary key.
	 */
	public MobileAuth getMobileAuth(Integer id) {
		MobileAuth m = dbe.load(SQL_GET_MOBILEAUTH_BY_PK, MobileAuth.class, id);
		return m;
	}

	/**
	 * Insert one row.
	 */
	public long insertMobileAuth(DOMobileAuth mobileAuth) {
		return dbe.insertWithAutoKey(SQL_INSERT_MOBILEAUTH, mobileAuth.getMobileNo(), mobileAuth.getCreateDate(),
				mobileAuth.getAuthTypeDsid(), mobileAuth.getUserUid(), mobileAuth.getEpToken(), mobileAuth.getAuthCode(),
				mobileAuth.getEndDate(), mobileAuth.getAuthedDate());
	}

	/**
	 * Update by PK.
	 */
	public void updateMobileAuth(DOMobileAuth mobileAuth) {
		dbe.execute(SQL_UPDATE_MOBILEAUTH, mobileAuth.getMobileNo(), mobileAuth.getCreateDate(), mobileAuth.getAuthTypeDsid(),
				mobileAuth.getUserUid(), mobileAuth.getEpToken(), mobileAuth.getAuthCode(), mobileAuth.getEndDate(),
				mobileAuth.getAuthedDate(), mobileAuth.getId());
	}

	/**
	 * Delete row by PK.
	 */
	public void deleteMobileAuth(Integer id) {
		dbe.execute(SQL_DEL_MOBILEAUTH, id);
	}

	public MobileAuth getMobileAuthByAuth(String mobileNo, String authCode, String token) {
		return dbe.load(SQL_GET_MOBILE_AUTH_BY_AUTH_3, MobileAuth.class, mobileNo, authCode, token);
	}

	public MobileAuth getMobileAuthByAuthNoToken(String mobileNo, String authCode) {
		return dbe.load(SQL_GET_MOBILE_AUTH_BY_AUTH_2, MobileAuth.class, mobileNo, authCode);
	}

}